/**
 * 打印组件
 */
import { getPrintInfo } from '@/api/supplier';
import { styleContent } from './printTemplateCSS';
export default {
  data() {
    return {
      printData: {},
      printLoading: false
    };
  },
  methods: {
    preview() {
      // 判断iframe是否存在，不存在则创建iframe
      var iframe = document.getElementById('print-iframe');
      if (iframe) {
        document.body.removeChild(iframe);
      }
      const printAreaContent = document.getElementById('printArea').innerHTML;
      iframe = document.createElement('IFRAME');
      let doc = null;
      iframe.setAttribute('id', 'print-iframe');
      iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;opacity: 0;');
      document.body.appendChild(iframe);
      doc = iframe.contentWindow.document;
      // 这里可以自定义样式
      doc.write(styleContent);
      doc.write('<div>' + printAreaContent + '</div>');
      doc.close();
      iframe.contentWindow.focus();
      iframe.contentWindow.print();
    },
    printTable() {
      document.getElementById('printArea').style.display = 'block';
      const examinId = this.examinId;
      this.printLoading = true;
      getPrintInfo({ id: examinId }).then(res => {
        this.printLoading = false;
        if (res.code === 200) {
          this.printData = res.data;
          if (this.printData.allocationList && this.printData.allocationList.length > 0) {
            this.printData.totalAllocationAmount = this.printData.allocationList.reduce((total, current) => {
              return total + Number(current.amount);
            }, 0) * 100;
            this.printData.totalAllocationAmount = (this.printData.totalAllocationAmount / 100).toFixed(2);
          }
          console.log(this.printData);
          this.$nextTick(() => {
            this.preview();
          });
        }
      }).catch(() => {
        this.printLoading = false;
        // this.$nextTick(() => {
        //   this.preview();
        // });
      });
    }
  }
};
